﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Sonic.Framework.ProcessorToolDirectives.Default
{
    public abstract class IPluginBase
    {
        protected string GetDirectiveCallAttributeFromArgXml(XmlDocument argumentsXml, string attributeName)
        {
            string result = null;

            try
            {
                result = argumentsXml.FirstChild.FirstChild.Attributes[attributeName].InnerText;
            }
            catch
            {
                result = null;
            }

            return result;
        }

        #region File path helpers

        /// <summary>
        /// Converts a path string to a relative path string. It replaces the tilder with the current execution directory.
        /// i.e. ~/file.exe --> C:\temp\file.exe
        /// </summary>
        /// <param name="path">Path to relativize</param>
        /// <returns>Full file system path. i.e. ~/file.exe --> C:\temp\file.exe</returns>
        protected string RelativizePath(string path)
        {
            if (path.StartsWith(@"/") || path.StartsWith(@"\"))
            {
                path = "~" + path;
            }

            if (path.StartsWith(@"~"))
            {
                string currentDirectory = System.IO.Directory.GetCurrentDirectory();
                path = CombinePaths(currentDirectory, path.Substring(1));
            }

            return path;
        }

        /// <summary>
        /// Converts a path string to a relative path string. It replaces the tilder with the root string variable
        /// i.e. ~/file.exe --> C:\temp\file.exe
        /// </summary>
        /// <param name="root">Root path</param>
        /// <param name="path">Path to relativize</param>
        /// <returns>Full file system path. i.e. ~\file.exe --> {root variable}\file.exe</returns>
        protected string RelativizePath(string root, string path)
        {
            if (path.StartsWith(@"/") || path.StartsWith(@"\"))
            {
                path = @"~" + path;
            }
            else if (path.StartsWith(@"~"))
            {
                path = path.Substring(1);
            }
            else
            {
                path = @"\" + path;
            }

            path = CombinePaths(root, path);

            return path;
        }

        /// <summary>
        /// A wrapper for System.IO.Path.Combine that makes it a bit more robust to preceeding slashes
        /// </summary>
        /// <param name="path1"></param>
        /// <param name="path2"></param>
        /// <returns>path1 combined with path2</returns>
        protected string CombinePaths(string path1, string path2)
        {
            if (path2[0] == '/' || path2[0] == '\\')
            {
                path2 = path2.Substring(1);
            }

            return System.IO.Path.Combine(path1, path2);
        }

        #endregion
    }
}
